
package com.example.testutil.util;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;

/**
 * 获取设备的基本信息
 * 
 * @author raymond
 */
public class DeviceInfoUtil {
    private static float[] mInfo;

    public DeviceInfoUtil() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 获取设备的宽,高,dpi,density
     */
    public static float[] getDeviceScreenInfo(Context mContext) {
        if (mInfo == null) {
            mInfo = new float[4];
        }
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        float screenWith = metrics.widthPixels;
        float screenHeight = metrics.heightPixels;
        float screenDensity = metrics.density;
        float densityDpi = metrics.densityDpi;
        mInfo[0] = screenWith;
        mInfo[1] = screenHeight;
        mInfo[2] = screenDensity;
        mInfo[3] = densityDpi;
        return mInfo;

    }

    /**
     * 获取设备的版本号
     * 
     * @return 设备系统版本号
     */
    public static int getSDKVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }

    /**
     * 获取屏幕旋转的角度
     * 
     * @param activity
     * @return 屏幕旋转的角度
     */
    public static int getSCreenRotation(Activity activity) {
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        switch (rotation) {
            default:
            case Surface.ROTATION_0:
                return 0;
            case Surface.ROTATION_90:
                return 90;
            case Surface.ROTATION_180:
                return 180;
            case Surface.ROTATION_270:
                return 270;
        }
    }

    /**
     * 获取屏幕的截图
     * 此截图不能截取电池状态部分
     * @param mActivity
     * @return
     */
    public static Bitmap getCaptrueWithStatusBar(Activity mActivity) {
        View view = mActivity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        DisplayMetrics metrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        Bitmap captureBitmap = Bitmap.createBitmap(bitmap, 0, 0, metrics.widthPixels, metrics.heightPixels);
        view.destroyDrawingCache();
        return captureBitmap;
    }
    /**
     * 判断是否锁屏
     * @return
     */
    public static boolean isScreenLock(Context mContext){
        KeyguardManager manager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
        return manager.isKeyguardLocked();
    }
}
